/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vita.vitasolution.service.util.converters;

import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import vita.vitasolution.db.beans.UserDAO;
import vita.vitasolution.db.entities.Case;
import vita.vitasolution.db.entities.Criterion;
import vita.vitasolution.db.entities.User;
import vita.vitasolution.service.dto.DetailedCase;
import vita.vitasolution.service.dto.ShortCriterion;

/**
 *
 * @author Iaroslav_Mazai
 */
@Named
public class CaseDetailedCaseConverter implements EntityDtoConverter<Case, DetailedCase> {

	private CriteriaShortCriteriaConverter criteriaShortCriteriaConverter;
	@Inject
	UserDAO userDAO;

	public CaseDetailedCaseConverter() {
		this.criteriaShortCriteriaConverter = new CriteriaShortCriteriaConverter();
	}

	@Override
	public Case getEntityFromDto(DetailedCase detailedCase) {
		Case theCase = new Case();

		theCase.setId(detailedCase.getId());
		theCase.setCopiesNumber(detailedCase.getCopiesNumber());
		theCase.setFund(detailedCase.getFund());
		theCase.setTitle(detailedCase.getTitle());
		User user = userDAO.findByID(detailedCase.getUserId());
		theCase.setUser(user);
		theCase.setPostDate(detailedCase.getPostDate());
		if (detailedCase.getCriteria() != null) {
			for (ShortCriterion shortCriteria : detailedCase.getCriteria()) {
				Criterion criterion = criteriaShortCriteriaConverter.getEntityFromDto(shortCriteria);
				theCase.addCriteria(criterion);
			}
		}

		return theCase;
	}

	@Override
	public DetailedCase getDtoFromEntity(Case theCase) {
		DetailedCase detailedCase = new DetailedCase();

		detailedCase.setId(theCase.getId());
		detailedCase.setCopiesNumber(theCase.getCopiesNumber());
		detailedCase.setFund(theCase.getFund());
		detailedCase.setTitle(theCase.getTitle());
		detailedCase.setUserId(theCase.getUser().getId());
		detailedCase.setPostDate(theCase.getPostDate());
		if (theCase.getCriteriaSet() != null) {
			Set<ShortCriterion> criteria = new HashSet<ShortCriterion>();
			for (Criterion criterion : theCase.getCriteriaSet()) {
				ShortCriterion shortCriteria = criteriaShortCriteriaConverter.getDtoFromEntity(criterion);
				criteria.add(shortCriteria);
			}
			detailedCase.setCriteria(criteria);
		}

		return detailedCase;
	}
}
